<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
    <el-form label-width="120px" :rules="rules" :model="form" ref="confirmForm">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          autocomplete="off"
          placeholder="请输入1~10个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          autocomplete="off"
          placeholder="请输入5~8个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          autocomplete="off"
          placeholder="请选择负责人"
          @focus="getEms"
        >
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="4"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnEnter"
          >确定</el-button
        >
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getDepartments,
  addDepartments,
  editDepartments,
  updateDepartments,
} from "@/api/departments";
import { getEmployees } from "@/api/employees";
export default {
  props: {
    pid: {
      required: true,
      type: String,
    },
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    let checkName = async (rule, value, callback) => {
      let res = await getDepartments();
      // console.log(res);
      //适用于编辑功能的规则
      if (this.form.id) {
        let list = res.data.depts.filter((item) => (item.pid = this.form.pid));
        let repeat = list.some((item) => {
          item.name == value && item.name != this.oldForm.name;
        });
        repeat ? callback(new Error("已有相同名字的部门,请修改!")) : callback();
      } else {
        //适用于新增功能的规则
        let list = res.data.depts.filter((item) => {
          // console.log(item);
          if (item.pid == this.pid) {
            return true;
          }
        });
        for (let i = 0; i < list.length; i++) {
          if (list[i].name === value) {
            return callback(new Error("已有相同名字的部门,请修改!"));
          }
        }
        callback();
      }
    };
    let checkCode = async (rule, value, callback) => {
      let res = await getDepartments();
      // console.log(res);
      let repeat;
      if (this.form.id) {
        repeat = res.data.depts.some(
          (item) => item.code === value && item.code != this.oldForm.code
        );
      } else {
        repeat = res.data.depts.some((item) => item.code === value);
      }
      repeat ? callback(new Error("部门编码重复,请更改!")) : callback();
    };
    return {
      // 定义一个表单,为空
      form: {},
      list: [],
      oldForm: {
        name: "",
        code: "",
      },
      // 增加路由规则
      rules: {
        name: [
          {
            validator: checkName,
            trigger: "blur",
          },
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          { min: 1, max: 10, message: "部门名称为1~10个字符", trigger: "blur" },
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {
            min: 3,
            max: 8,
            message: "部门编码只能为5~8个数字",
            trigger: "blur",
          },
          { validator: checkCode, trigger: "blur" },
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {
            min: 1,
            max: 300,
            message: "部门介绍需在1~300个字符间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    showTitle() {
      return this.form.id ? "编辑部门" : "新增子部门";
    },
  },
  methods: {
    async getEms() {
      let res = await getEmployees();
      // console.log(res);
      this.list = res.data;
      // console.log(this.list);
    },
    btnEnter() {
      this.$refs.confirmForm.validate(async (v) => {
        if (v) {
          if (this.form.id) {
            await updateDepartments(this.form);
            this.$message.success("修改信息成功!");
          } else {
            await addDepartments({
              ...this.form,
              pid: this.pid,
            });
            this.$message.success("新增部门成功!");
          }
          this.$emit("addDepts");
          this.$emit("update:showDialog", false);
        }
      });
    },
    btnCancel() {
      this.form = {};
      this.$emit("update:showDialog", false);
      this.$refs.confirmForm.resetFields();
    },
    async editDepartments(id) {
      let res = await editDepartments(id);
      // console.log(res);
      this.form = res.data;
      this.oldForm.name = res.data.name;
      this.oldForm.code = res.data.code;
    },
  },
};
</script>

<style>
.el-button {
}
</style>